﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using SaleManagement.Domains;
using SaleManagement.Data;

#endregion

namespace SaleManagement.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ExportProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ExportProviderBaseCore : EntityProviderBase<SaleManagement.Domains.Export, SaleManagement.Domains.ExportKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, SaleManagement.Domains.ExportKey key)
		{
			return Delete(transactionManager, key.Id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _id)
		{
			return Delete(null, _id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _id);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		FK_tblExport_Repository2 Description: 
		/// </summary>
		/// <param name="_toRepositoryId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByToRepositoryId(System.Int32? _toRepositoryId)
		{
			int count = -1;
			return GetByToRepositoryId(_toRepositoryId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		FK_tblExport_Repository2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_toRepositoryId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		/// <remarks></remarks>
		public TList<Export> GetByToRepositoryId(TransactionManager transactionManager, System.Int32? _toRepositoryId)
		{
			int count = -1;
			return GetByToRepositoryId(transactionManager, _toRepositoryId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		FK_tblExport_Repository2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_toRepositoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByToRepositoryId(TransactionManager transactionManager, System.Int32? _toRepositoryId, int start, int pageLength)
		{
			int count = -1;
			return GetByToRepositoryId(transactionManager, _toRepositoryId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		fkTblExportRepository2 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_toRepositoryId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByToRepositoryId(System.Int32? _toRepositoryId, int start, int pageLength)
		{
			int count =  -1;
			return GetByToRepositoryId(null, _toRepositoryId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		fkTblExportRepository2 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_toRepositoryId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByToRepositoryId(System.Int32? _toRepositoryId, int start, int pageLength,out int count)
		{
			return GetByToRepositoryId(null, _toRepositoryId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository2 key.
		///		FK_tblExport_Repository2 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_toRepositoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public abstract TList<Export> GetByToRepositoryId(TransactionManager transactionManager, System.Int32? _toRepositoryId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		FK_tblExport_Repository1 Description: 
		/// </summary>
		/// <param name="_fromRepositoryId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByFromRepositoryId(System.Int32 _fromRepositoryId)
		{
			int count = -1;
			return GetByFromRepositoryId(_fromRepositoryId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		FK_tblExport_Repository1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_fromRepositoryId"></param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		/// <remarks></remarks>
		public TList<Export> GetByFromRepositoryId(TransactionManager transactionManager, System.Int32 _fromRepositoryId)
		{
			int count = -1;
			return GetByFromRepositoryId(transactionManager, _fromRepositoryId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		FK_tblExport_Repository1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_fromRepositoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByFromRepositoryId(TransactionManager transactionManager, System.Int32 _fromRepositoryId, int start, int pageLength)
		{
			int count = -1;
			return GetByFromRepositoryId(transactionManager, _fromRepositoryId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		fkTblExportRepository1 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_fromRepositoryId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByFromRepositoryId(System.Int32 _fromRepositoryId, int start, int pageLength)
		{
			int count =  -1;
			return GetByFromRepositoryId(null, _fromRepositoryId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		fkTblExportRepository1 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_fromRepositoryId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public TList<Export> GetByFromRepositoryId(System.Int32 _fromRepositoryId, int start, int pageLength,out int count)
		{
			return GetByFromRepositoryId(null, _fromRepositoryId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_tblExport_Repository1 key.
		///		FK_tblExport_Repository1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_fromRepositoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SaleManagement.Domains.Export objects.</returns>
		public abstract TList<Export> GetByFromRepositoryId(TransactionManager transactionManager, System.Int32 _fromRepositoryId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override SaleManagement.Domains.Export Get(TransactionManager transactionManager, SaleManagement.Domains.ExportKey key, int start, int pageLength)
		{
			return GetById(transactionManager, key.Id, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_tblExport index.
		/// </summary>
		/// <param name="_id"></param>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public SaleManagement.Domains.Export GetById(System.Int32 _id)
		{
			int count = -1;
			return GetById(null,_id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblExport index.
		/// </summary>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public SaleManagement.Domains.Export GetById(System.Int32 _id, int start, int pageLength)
		{
			int count = -1;
			return GetById(null, _id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblExport index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public SaleManagement.Domains.Export GetById(TransactionManager transactionManager, System.Int32 _id)
		{
			int count = -1;
			return GetById(transactionManager, _id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblExport index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public SaleManagement.Domains.Export GetById(TransactionManager transactionManager, System.Int32 _id, int start, int pageLength)
		{
			int count = -1;
			return GetById(transactionManager, _id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblExport index.
		/// </summary>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public SaleManagement.Domains.Export GetById(System.Int32 _id, int start, int pageLength, out int count)
		{
			return GetById(null, _id, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tblExport index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="SaleManagement.Domains.Export"/> class.</returns>
		public abstract SaleManagement.Domains.Export GetById(TransactionManager transactionManager, System.Int32 _id, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _tblExport_Search 
		
		/// <summary>
		///	This method wrap the '_tblExport_Search' stored procedure. 
		/// </summary>
		/// <param name="fromDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="toDate"> A <c>System.DateTime?</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Search(System.DateTime? fromDate, System.DateTime? toDate)
		{
			return Search(null, 0, int.MaxValue , fromDate, toDate);
		}
		
		/// <summary>
		///	This method wrap the '_tblExport_Search' stored procedure. 
		/// </summary>
		/// <param name="fromDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="toDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Search(int start, int pageLength, System.DateTime? fromDate, System.DateTime? toDate)
		{
			return Search(null, start, pageLength , fromDate, toDate);
		}
				
		/// <summary>
		///	This method wrap the '_tblExport_Search' stored procedure. 
		/// </summary>
		/// <param name="fromDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="toDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Search(TransactionManager transactionManager, System.DateTime? fromDate, System.DateTime? toDate)
		{
			return Search(transactionManager, 0, int.MaxValue , fromDate, toDate);
		}
		
		/// <summary>
		///	This method wrap the '_tblExport_Search' stored procedure. 
		/// </summary>
		/// <param name="fromDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="toDate"> A <c>System.DateTime?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public abstract DataSet Search(TransactionManager transactionManager, int start, int pageLength , System.DateTime? fromDate, System.DateTime? toDate);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Export&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Export&gt;"/></returns>
		public static TList<Export> Fill(IDataReader reader, TList<Export> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				SaleManagement.Domains.Export c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Export")
					.Append("|").Append((System.Int32)reader[((int)ExportColumn.Id - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Export>(
					key.ToString(), // EntityTrackingKey
					"Export",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new SaleManagement.Domains.Export();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.Id = (System.Int32)reader[((int)ExportColumn.Id - 1)];
					c.RefNo = (reader.IsDBNull(((int)ExportColumn.RefNo - 1)))?null:(System.String)reader[((int)ExportColumn.RefNo - 1)];
					c.FromRepositoryId = (System.Int32)reader[((int)ExportColumn.FromRepositoryId - 1)];
					c.ToRepositoryId = (reader.IsDBNull(((int)ExportColumn.ToRepositoryId - 1)))?null:(System.Int32?)reader[((int)ExportColumn.ToRepositoryId - 1)];
					c.ExportDate = (System.DateTime)reader[((int)ExportColumn.ExportDate - 1)];
					c.TotalAmount = (reader.IsDBNull(((int)ExportColumn.TotalAmount - 1)))?null:(System.Int32?)reader[((int)ExportColumn.TotalAmount - 1)];
					c.Notes = (reader.IsDBNull(((int)ExportColumn.Notes - 1)))?null:(System.String)reader[((int)ExportColumn.Notes - 1)];
					c.UpdatedDate = (reader.IsDBNull(((int)ExportColumn.UpdatedDate - 1)))?null:(System.DateTime?)reader[((int)ExportColumn.UpdatedDate - 1)];
					c.UpdatedBy = (reader.IsDBNull(((int)ExportColumn.UpdatedBy - 1)))?null:(System.String)reader[((int)ExportColumn.UpdatedBy - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="SaleManagement.Domains.Export"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.Export"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, SaleManagement.Domains.Export entity)
		{
			if (!reader.Read()) return;
			
			entity.Id = (System.Int32)reader[((int)ExportColumn.Id - 1)];
			entity.RefNo = (reader.IsDBNull(((int)ExportColumn.RefNo - 1)))?null:(System.String)reader[((int)ExportColumn.RefNo - 1)];
			entity.FromRepositoryId = (System.Int32)reader[((int)ExportColumn.FromRepositoryId - 1)];
			entity.ToRepositoryId = (reader.IsDBNull(((int)ExportColumn.ToRepositoryId - 1)))?null:(System.Int32?)reader[((int)ExportColumn.ToRepositoryId - 1)];
			entity.ExportDate = (System.DateTime)reader[((int)ExportColumn.ExportDate - 1)];
			entity.TotalAmount = (reader.IsDBNull(((int)ExportColumn.TotalAmount - 1)))?null:(System.Int32?)reader[((int)ExportColumn.TotalAmount - 1)];
			entity.Notes = (reader.IsDBNull(((int)ExportColumn.Notes - 1)))?null:(System.String)reader[((int)ExportColumn.Notes - 1)];
			entity.UpdatedDate = (reader.IsDBNull(((int)ExportColumn.UpdatedDate - 1)))?null:(System.DateTime?)reader[((int)ExportColumn.UpdatedDate - 1)];
			entity.UpdatedBy = (reader.IsDBNull(((int)ExportColumn.UpdatedBy - 1)))?null:(System.String)reader[((int)ExportColumn.UpdatedBy - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="SaleManagement.Domains.Export"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.Export"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, SaleManagement.Domains.Export entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (System.Int32)dataRow["Id"];
			entity.RefNo = Convert.IsDBNull(dataRow["RefNo"]) ? null : (System.String)dataRow["RefNo"];
			entity.FromRepositoryId = (System.Int32)dataRow["FromRepositoryId"];
			entity.ToRepositoryId = Convert.IsDBNull(dataRow["ToRepositoryId"]) ? null : (System.Int32?)dataRow["ToRepositoryId"];
			entity.ExportDate = (System.DateTime)dataRow["ExportDate"];
			entity.TotalAmount = Convert.IsDBNull(dataRow["TotalAmount"]) ? null : (System.Int32?)dataRow["TotalAmount"];
			entity.Notes = Convert.IsDBNull(dataRow["Notes"]) ? null : (System.String)dataRow["Notes"];
			entity.UpdatedDate = Convert.IsDBNull(dataRow["UpdatedDate"]) ? null : (System.DateTime?)dataRow["UpdatedDate"];
			entity.UpdatedBy = Convert.IsDBNull(dataRow["UpdatedBy"]) ? null : (System.String)dataRow["UpdatedBy"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="SaleManagement.Domains.Export"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">SaleManagement.Domains.Export Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, SaleManagement.Domains.Export entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ToRepositoryIdSource	
			if (CanDeepLoad(entity, "Repository|ToRepositoryIdSource", deepLoadType, innerList) 
				&& entity.ToRepositoryIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.ToRepositoryId ?? (int)0);
				Repository tmpEntity = EntityManager.LocateEntity<Repository>(EntityLocator.ConstructKeyFromPkItems(typeof(Repository), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ToRepositoryIdSource = tmpEntity;
				else
					entity.ToRepositoryIdSource = DataRepository.RepositoryProvider.GetById(transactionManager, (entity.ToRepositoryId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ToRepositoryIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ToRepositoryIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.RepositoryProvider.DeepLoad(transactionManager, entity.ToRepositoryIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ToRepositoryIdSource

			#region FromRepositoryIdSource	
			if (CanDeepLoad(entity, "Repository|FromRepositoryIdSource", deepLoadType, innerList) 
				&& entity.FromRepositoryIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.FromRepositoryId;
				Repository tmpEntity = EntityManager.LocateEntity<Repository>(EntityLocator.ConstructKeyFromPkItems(typeof(Repository), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.FromRepositoryIdSource = tmpEntity;
				else
					entity.FromRepositoryIdSource = DataRepository.RepositoryProvider.GetById(transactionManager, entity.FromRepositoryId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'FromRepositoryIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.FromRepositoryIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.RepositoryProvider.DeepLoad(transactionManager, entity.FromRepositoryIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion FromRepositoryIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetById methods when available
			
			#region ExportDetailCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ExportDetail>|ExportDetailCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ExportDetailCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ExportDetailCollection = DataRepository.ExportDetailProvider.GetByExportId(transactionManager, entity.Id);

				if (deep && entity.ExportDetailCollection.Count > 0)
				{
					deepHandles.Add("ExportDetailCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ExportDetail>) DataRepository.ExportDetailProvider.DeepLoad,
						new object[] { transactionManager, entity.ExportDetailCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the SaleManagement.Domains.Export object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">SaleManagement.Domains.Export instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">SaleManagement.Domains.Export Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, SaleManagement.Domains.Export entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ToRepositoryIdSource
			if (CanDeepSave(entity, "Repository|ToRepositoryIdSource", deepSaveType, innerList) 
				&& entity.ToRepositoryIdSource != null)
			{
				DataRepository.RepositoryProvider.Save(transactionManager, entity.ToRepositoryIdSource);
				entity.ToRepositoryId = entity.ToRepositoryIdSource.Id;
			}
			#endregion 
			
			#region FromRepositoryIdSource
			if (CanDeepSave(entity, "Repository|FromRepositoryIdSource", deepSaveType, innerList) 
				&& entity.FromRepositoryIdSource != null)
			{
				DataRepository.RepositoryProvider.Save(transactionManager, entity.FromRepositoryIdSource);
				entity.FromRepositoryId = entity.FromRepositoryIdSource.Id;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<ExportDetail>
				if (CanDeepSave(entity.ExportDetailCollection, "List<ExportDetail>|ExportDetailCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ExportDetail child in entity.ExportDetailCollection)
					{
						if(child.ExportIdSource != null)
						{
							child.ExportId = child.ExportIdSource.Id;
						}
						else
						{
							child.ExportId = entity.Id;
						}

					}

					if (entity.ExportDetailCollection.Count > 0 || entity.ExportDetailCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ExportDetailProvider.Save(transactionManager, entity.ExportDetailCollection);
						
						deepHandles.Add("ExportDetailCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ExportDetail >) DataRepository.ExportDetailProvider.DeepSave,
							new object[] { transactionManager, entity.ExportDetailCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ExportChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>SaleManagement.Domains.Export</c>
	///</summary>
	public enum ExportChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Repository</c> at ToRepositoryIdSource
		///</summary>
		[ChildEntityType(typeof(Repository))]
		Repository,
	
		///<summary>
		/// Collection of <c>Export</c> as OneToMany for ExportDetailCollection
		///</summary>
		[ChildEntityType(typeof(TList<ExportDetail>))]
		ExportDetailCollection,
	}
	
	#endregion ExportChildEntityTypes
	
	#region ExportFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ExportColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Export"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ExportFilterBuilder : SqlFilterBuilder<ExportColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ExportFilterBuilder class.
		/// </summary>
		public ExportFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ExportFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ExportFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ExportFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ExportFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ExportFilterBuilder
	
	#region ExportParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ExportColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Export"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ExportParameterBuilder : ParameterizedSqlFilterBuilder<ExportColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ExportParameterBuilder class.
		/// </summary>
		public ExportParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ExportParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ExportParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ExportParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ExportParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ExportParameterBuilder
	
	#region ExportSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ExportColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Export"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ExportSortBuilder : SqlSortBuilder<ExportColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ExportSqlSortBuilder class.
		/// </summary>
		public ExportSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ExportSortBuilder
	
} // end namespace
